package utils;

import java.util.Enumeration;
import java.util.Observable;

import commands.Commands;

import nanoxml.XMLElement;

/**
 * The different tables and users available.
 */
public class ServerInfo extends Observable implements ITransmittable {

	private TableList tableList;
	private UserList userList;
	
	public ServerInfo(){
		tableList = new TableList();
		userList  = new UserList();
	}
	
	@SuppressWarnings("unchecked")
	public ServerInfo (XMLElement message) {

		Enumeration<XMLElement> e = message.enumerateChildren();
		while (e.hasMoreElements()) {
			XMLElement childMessage = e.nextElement();
			if (childMessage.getName().equals (Commands.USER_LIST)) {
				this.userList = new UserList (childMessage);
			}
			else if (childMessage.getName().equals (Commands.TABLE_LIST)) {
				this.tableList = new TableList(childMessage);
			}
		}

		// If no users / tables - create empty user and table lists
		if (userList == null)
		    userList = new UserList ();
		if (tableList == null)
		    tableList = new TableList ();
	}
	
	public XMLElement toXML() {
        XMLElement message = new XMLElement ();
        message.setName(Commands.SERVER_INFO);
        if (userList.size() > 0)
            message.addChild (userList.toXML());
         if (tableList.size() > 0)
            message.addChild (tableList.toXML());
        return message;
    }

	public UserList getUserList() {
		return userList;
	}
	
	public TableList getTableList() {
		return tableList;
	}
}
